package com.shangma.controller;

import com.shangma.entity.Employee;
import com.shangma.service.EmployeeService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    @GetMapping("writeExcel")
    public ResponseEntity<byte[]> findAll() throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 1.拿到数据
        List<Employee> employees = employeeService.findAll();
        // 2.创建workBook
        Workbook workbook = new XSSFWorkbook();
        // 3.创建sheet
        Sheet sheet = workbook.createSheet("员工信息表");
        // 添加标题
        Row titleRow = sheet.createRow(0);
        List<String> strings = Arrays.asList("员工ID","员工名称","员工地址","手机号码","入职时间","密码","员工薪资","头像地址");
        for (int i = 0; i < strings.size(); i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(strings.get(i));
        }
        // 4.创建行
        for (int i = 0;i < employees.size();i++){
            Row row = sheet.createRow(i + 1);
            Employee employee = employees.get(i);
            // 5.创建每一行中的单元格并且赋值
            Cell idCell = row.createCell(0);
            idCell.setCellValue(employee.getEmployeeId());
            Cell nameCell = row.createCell(1);
            nameCell.setCellValue(employee.getEmployeeName());
            Cell addressCell = row.createCell(2);
            addressCell.setCellValue(employee.getEmployeeAddress());
            Cell phoneCell = row.createCell(3);
            phoneCell.setCellValue(employee.getEmployeePhone());
            Cell timeCell = row.createCell(4);
            timeCell.setCellValue(sdf.format(employee.getEmployeeTime()));
            Cell passwordCell = row.createCell(5);
            passwordCell.setCellValue(employee.getEmployeePassword());
            Cell salaryCell = row.createCell(6);
            salaryCell.setCellValue(employee.getEmployeeSalary());
            Cell avatarCell = row.createCell(7);
            avatarCell.setCellValue(employee.getEmployeeAvatar());
        }

        // 6.输出到本地
//        FileOutputStream out = new FileOutputStream("D:\\employee.xlsx");
//        workbook.write(out);
//        out.close();
//        workbook.close();
        // 写入到内存中
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        workbook.write(stream);
        // 设置头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", URLEncoder.encode("员工信息表.xlsx","utf-8"));
        byte[] bytes = stream.toByteArray();
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes,httpHeaders, HttpStatus.OK);
        return responseEntity;
    }
}
